<script setup lang="ts">
import { ref, onMounted } from "vue";
import { encrypt, decrypt } from "@/utils/crypto";
import { storage } from "@/utils/storage";
import { checkAuthStatus } from "@/utils/auth";
import * as api from "@/api/index";
import { useUserStore } from "@/stores/modules/user";
import { ConcurrentRequest } from "@/utils/concurrent/request";
import { http } from "@/api/http/request";
import { API_URLS } from "@/api/constants/api-url";
import * as util from "@/core/shared/utils/util";


const userStore = useUserStore();
const TOKEN_KEY = "accessToken";
const REFRESH_TOKEN_KEY = "refreshToken";

onMounted(async () => {});
// --token验证测试--
const handleAuthClick = async () => {
  const res = await http.get(API_URLS.AUTH_VERIFY_TOKEN, {
    skipAuth: false,
    cache: { enable: false },
  });
  console.log(res);
  return;
  const { isValid, reason } = await checkAuthStatus();
  console.log(isValid, reason);
  if (!isValid) {
    console.log(reason);
  }
};

// --加密解密测试--
const handleEncryptClick = () => {
  const data = "123456";
  const encrypted = encrypt(data);
  console.log("Encrypted:", encrypted);
  const decrypted = decrypt(encrypted);
  console.log("Decrypted:", decrypted);
  console.log(
    "Decrypted:",
    decrypt(
      "U2FsdGVkX188WmEe4wrePPzz5Zqcf+bG/koDADdeWDnWrc+P+a7kruZGqALoG9/+VIiriw6hMDpTt0u9mIvZWSGWSCv33MDOyhRHB7Sr9mVAF53ey41zCh3VBzj+NHuBMAg67BrdD7TsYN2Ga3gKnI7pO8WzEMNarEnWGPxOaCHdgDXrmYUWsVj4Y2UzlhritjBR1I30sntD+Py3lLowR+bMIT/fCKHgfWdYEnWc6e50KXoS0gr/xjEdsARlFr6I"
    )
  );
};

// --测试缓存--
const handleCacheClick = () => {
  const token = storage.get("accessToken");
  console.log("授权token：", token);
  const cacheKey = "test_cache";
  const cacheValue = "test_value";
  localStorage.setItem(cacheKey, cacheValue);
  const cachedValue = localStorage.getItem(cacheKey);
  console.log("Cached value:", cachedValue);
};

// 获取当前用户信息，测试刷新token功能
const handleUserInfoClick = async () => {
  const res = await api.getCurrUserInfo();
  console.log("当前用户信息:", res);
};

const handleRefreshTokenClick = async () => {
  const token: any = storage.get("refreshToken");
  const res = await api.refreshToken({
    refreshToken:
      "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNzUzNzA2NTA1LCJleHAiOjE3NTQzMTEzMDV9.eWp2KVhl7rYmgHeRAfiLV6EcQkpS3M4ixG4F7ofRmYs",
  });
  console.log("刷新token结果：", res);
  userStore.setToken(res.data.access_token, res.data.refresh_token);
};

// 模拟异步请求
function mockRequest(url: string, delay: number) {
  return () =>
    new Promise((resolve) => {
      console.log(`开始请求: ${url}`);
      setTimeout(() => {
        console.log(`请求完成: ${url}`);
        resolve(`结果: ${url}`);
      }, delay);
    });
}
// 测试并发请求
const handleConcurrentRequestClick = async () => {
  const concurrentRequest = new ConcurrentRequest();

  concurrentRequest.add(mockRequest("/api/1", 1000)).then(console.log);
  concurrentRequest.add(mockRequest("/api/2", 1500)).then(console.log);
  concurrentRequest.add(mockRequest("/api/3", 500)).then(console.log);
  concurrentRequest.add(mockRequest("/api/4", 800)).then(console.log);
  concurrentRequest.add(mockRequest("/api/5", 1200)).then(console.log);
  concurrentRequest.add(mockRequest("/api/6", 1800)).then(console.log);
  concurrentRequest.add(mockRequest("/api/7", 2000)).then(console.log);
  concurrentRequest.add(mockRequest("/api/8", 2500)).then(console.log);
  concurrentRequest.add(mockRequest("/api/9", 3000)).then(console.log);
  concurrentRequest.add(mockRequest("/api/10", 3500)).then(console.log);

  concurrentRequest._run();
};

const handleCategoryClick = async () => {
  const res = await api.getCategoryList();
  console.log("分类列表：", res);
  const treeData = res;
  const tree = util.buildTree(treeData);
  console.log("树形结构：", tree);
};
</script>
<template>
  <div class="p-5">
    <div class="flex justify-center items-center">
      <h3 class="text-2xl font-bold pb-15">接口、模块测试</h3>
    </div>

    <div class="flex justify-center items-center flex-wrap">
      <el-button @click="handleAuthClick">token验证测试</el-button>
      <el-button @click="handleEncryptClick">加密解密测试</el-button>
      <el-button @click="handleCacheClick">缓存测试</el-button>
      <el-button @click="handleUserInfoClick">获取用户信息</el-button>
      <el-button @click="handleRefreshTokenClick">测试刷新token功能</el-button>
      <el-button @click="handleConcurrentRequestClick">测试并发请求</el-button>
      <el-button @click="handleCategoryClick">测试分类转换功能</el-button>
    </div>

    <div class="text-white break-words">
      {{ userStore.accessToken }}
    </div>
    <div class="text-white break-words">
      {{ userStore.refreshToken }}
    </div>
  </div>
</template>
